package com.fzc.rhinooffice.module.workbench;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.fzc.rhinooffice.R;
import com.fzc.rhinooffice.module.BaseActivity;
import com.fzc.rhinooffice.module.entity.SortModel;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 邮件联系人列表
 * @author chao.liu
 *
 */
@SuppressLint("NewApi")
public class ContactsActivity extends BaseActivity implements OnClickListener{
	
	@ViewInject(R.id.iv_back)
	private ImageView iv_back;
	
	@ViewInject(R.id.tv_title)
	private TextView tv_title;
	
	@ViewInject(R.id.tv_filter)
	private TextView tv_filter;
	
	@ViewInject(R.id.lv_contacts)
	private ListView lv_contacts;
	
	/**
	 * 汉字转换成拼音的类
	 */
	private CharacterParser characterParser;
	private List<SortModel> SourceDateList;
	
	private SortAdapter contactsSortAdapter;
	
	/**
	 * 根据拼音来排列ListView里面的数据类
	 */
	private PinyinComparator pinyinComparator;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initUI();
		listener();
	}
	
	private void initUI(){
		setContentView(R.layout.activity_contacts);
		ViewUtils.inject(this);
		tv_title.setText("选择联系人");
		tv_filter.setText("确定");
		tv_filter.setBackground(null);
		
		pinyinComparator = new PinyinComparator();
		
		initAdapter();
	}
	
	private void listener(){
		iv_back.setOnClickListener(this);
		tv_filter.setOnClickListener(this);
	}
	
	
	
	private void initAdapter(){
		
		SourceDateList = new ArrayList<SortModel>();
		for(int i=0;i<5;i++){
			SortModel sm = new SortModel("nihao","n");
			SortModel sm2 = new SortModel("xiaokai","x");
			SourceDateList.add(sm);
			SourceDateList.add(sm2);
		}
		
		SourceDateList = filledData(SourceDateList);
		Collections.sort(SourceDateList,pinyinComparator);	//排序
		if(contactsSortAdapter==null){
			
			contactsSortAdapter = new SortAdapter(this, SourceDateList);
			lv_contacts.setAdapter(contactsSortAdapter);
		}else{
			contactsSortAdapter.updateListView(SourceDateList);
		}
		
	} 
	
	/**
	 * 为ListView填充数据
	 * @param date
	 * @return
	 */
	private List<SortModel> filledData(List<SortModel> list){
		List<SortModel> mSortList = new ArrayList<SortModel>();
		
		for(int i=0; i<list.size(); i++){
			SortModel sortModel = new SortModel();
			sortModel.setName(list.get(i).getName());
			//汉字转换成拼音
			//String pinyin = characterParser.getSelling(list.get(i).getName());
			//String sortString = pinyin.substring(0, 1).toUpperCase();
			
			String sortString = list.get(i).getName().substring(0, 1).toUpperCase();
			
			// 正则表达式，判断首字母是否是英文字母
			if(sortString.matches("[A-Z]")){
				sortModel.setSortLetters(sortString.toUpperCase());
			}else{
				sortModel.setSortLetters("#");
			}
			
			mSortList.add(sortModel);
		}
		return mSortList;
		
	}


	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.iv_back:
			super.back();
			break;

		case R.id.tv_filter:
			//添加
			HashMap<Integer, Boolean> hm = (HashMap<Integer, Boolean>) contactsSortAdapter.getIsCheckedMap();
			List<SortModel> result = new ArrayList<SortModel>();
			if(hm!=null && hm.size()!=0){
				for(int i=0;i<SourceDateList.size();i++){
					if(hm.containsKey(i)){
						result.add(SourceDateList.get(i));
					}
				}
			}
			Intent mIntent = new Intent();
			mIntent.putExtra("result", (Serializable)result);
			setResult(0, mIntent);
			finish();
			break;
		}
		
	}
	
}
